<template>
  <div
    class="page-read-cover"
    v-if="coverShow"
    :style="{ transform: `translateY(${translate}px)` }"
    @touchstart="touchStart"
    @touchmove="touchMove"
    @touchend="touchEnd"
  >
    <div class="read-cover-v">
      <i class="read-cover-cor"></i>
      <i class="read-cover-cor"></i>
      <i class="read-cover-cor"></i>
      <i class="read-cover-cor"></i>
      <div class="read-cover-info">
        <figure class="read-cover-figure">
          <img :src="bookDetail.image" class="read-cover-img" :alt="bookDetail.name" />
        </figure>
        <h2 class="read-cover-title ell">{{ bookDetail.name }}</h2>
        <p class="read-cover-author read-gray">{{ bookDetail.writer }} 著</p>

        <table class="read-cover-data">
          <tbody>
            <tr>
              <td width="30%">
                <strong>{{ formatType(bookDetail.is_over) }}</strong>
                <h6 class="read-gray">类型</h6>
              </td>
              <td>
                <strong>{{ formatIsOver(bookDetail.type) }}</strong>
                <h6 class="read-gray">状态</h6>
              </td>
              <td width="30%">
                <strong>{{ bookDetail.chapter_count }}</strong>
                <h6 class="read-gray">章节数</h6>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="read-cover-copy">
        <p>本站所收录小说作品属其个人行为</p>
        <p>不代表本站立场</p>
      </div>
    </div>
    <div class="read-cover-h"></div>
  </div>
</template>

<script lang="ts" setup>
import { PropType, defineProps, ref, Ref } from 'vue'
import { BookDetail } from '@/common/types'
import { overTypeMap, bookTypeMap } from '@/utils/bookMap'

const props = defineProps({
  bookDetail: {
    type: Object as PropType<BookDetail>,
    default: () => {
      return {
        name: '未知',
        image: '',
        writer: '匿名',
        type: 0,
        is_over: 0,
        chapter_count: 0
      }
    }
  }
})

let startX: number = 0
let startY: number = 0
let endX: number = 0
let endY: number = 0
const translate: Ref<number> = ref(0)
const coverShow: Ref<boolean> = ref(true)

const coverScrollToOut = async () => {
  if (translate.value > -window.screen.height) {
    await new Promise((resolve) =>
      window.requestAnimationFrame(() => {
        resolve((translate.value -= 30))
      })
    )
    coverScrollToOut()
  }
  coverShow.value = false
}

const touchStart = (event: TouchEvent) => {
  event.preventDefault()
  startX = event.targetTouches[0].screenX
  startY = event.targetTouches[0].screenY
}

const touchMove = (event: TouchEvent) => {
  event.preventDefault()
  endX = event.targetTouches[0].screenX
  endY = event.targetTouches[0].screenY
  translate.value = endY - startY > 0 ? 0 : endY - startY
}

const touchEnd = (event: TouchEvent) => {
  event.preventDefault()
  if (translate.value < -100) {
    coverScrollToOut()
  } else {
    translate.value = 0
  }
}

const formatIsOver = (is_over: number) => {
  return overTypeMap[is_over] || '未知'
}

const formatType = (type: number) => {
  return bookTypeMap[type] || '未知'
}
</script>

<style lang="scss" scoped>
.page-read-cover {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #c4b395 url('../../../../assets/images/skin-default.jpg') no-repeat center;
  background-size: 100% 100%;
  z-index: 99;
  .read-cover-v {
    position: absolute;
    top: 16px;
    right: 21px;
    bottom: 16px;
    left: 21px;
    text-align: center;
    border-top: 1px solid rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    white-space: nowrap;
    .read-cover-cor {
      position: absolute;
      width: 10px;
      height: 10px;
      text-align: left;
      &::after,
      &::before {
        position: absolute;
        content: '';
        border: 1px solid rgba(0, 0, 0, 0.2);
      }
      &::before {
        width: 5px;
        height: 5px;
        border-width: 0 1px 1px 0;
      }
      &::after {
        top: 5px;
        left: 5px;
        width: 3px;
        height: 3px;
        border-radius: 0 1px 1px 1px;
      }
      &:nth-of-type(1) {
        top: -1px;
        left: -5px;
      }
      &:nth-of-type(2) {
        top: -1px;
        right: -5px;
        transform: scaleX(-1);
      }
      &:nth-of-type(3) {
        right: -5px;
        bottom: -1px;
        transform: scale(-1, -1);
      }
      &:nth-of-type(4) {
        bottom: -1px;
        left: -5px;
        transform: scaleY(-1);
      }
    }
    .read-cover-info {
      display: inline-block;
      width: 100%;
      vertical-align: middle;
      margin-top: 20%;
      .read-cover-figure {
        display: inline-block;
        padding: 3px;
        border: 1px solid #efece5;
        background-color: #fcfcfa;
        .read-cover-img {
          display: block;
          width: 90px;
          height: 120px;
        }
      }
      .read-cover-title {
        font-size: 24px;
        font-weight: 700;
      }
      .read-cover-author {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.4);
      }
      .read-cover-data {
        width: 100%;
        margin-top: 20px;
      }
    }
    .read-cover-copy {
      font-size: 14px;
      position: absolute;
      right: 0;
      bottom: 10px;
      left: 0;
    }
  }
  .read-cover-h {
    position: absolute;
    top: 21px;
    right: 16px;
    bottom: 21px;
    left: 16px;
    border-right: 1px solid rgba(0, 0, 0, 0.2);
    border-left: 1px solid rgba(0, 0, 0, 0.2);
  }
}
</style>
